package org.wk.core.web.exception;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import org.wk.core.common.view.ExceptionVo;
import org.wk.core.web.tool.JSONUtils;
/**
 * 
 * @ClassName	: ExceptionHandlerAdvice
 * @Package		：org.wk.core.web.exception
 * @Author df.w 
 * @Create Time	：2018年5月2日 上午11:07:08
 * @Description	: 异常处理拦截
 */
@ControllerAdvice
public class ExceptionHandlerAdvice extends ResponseEntityExceptionHandler{
	private static final Logger logger = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(value = {GeneralException.class,IllegalArgumentException.class})
	public ModelAndView otherExceptionHandle(HttpServletRequest request,Exception exception) {
		logger.error(ExceptionUtils.getStackTrace(exception));
		String requestType = request.getHeader("X-Requested-With"); 
		String message = "";
		if(exception instanceof GeneralException){
			GeneralException generalException = (GeneralException) exception;
			message= generalException.getMessage();
		}
		if(exception instanceof IllegalArgumentException){
			IllegalArgumentException illegalArgumentException = (IllegalArgumentException) exception;
			message = "{\"code\":\"form_error\",\"message\":\""+illegalArgumentException.getMessage()+"\"}";
		}
		ModelAndView modelAndView = null;
		if(requestType!=null){
			ExceptionVo exceptionVo = JSONUtils.jsonCovertBean(message, ExceptionVo.class);
			MappingJackson2JsonView mappingJackson2JsonView = new MappingJackson2JsonView();
			modelAndView = new ModelAndView (mappingJackson2JsonView);
			modelAndView.addObject("code", exceptionVo.getCode());
			modelAndView.addObject("message", exceptionVo.getMessage());
		}else{
			modelAndView = new ModelAndView ("/404.html");
		}
		return modelAndView;
	}
	
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(value = {Throwable.class})
	public ModelAndView allExceptionHandle(HttpServletRequest request,Throwable exception) {
		logger.error(ExceptionUtils.getStackTrace(exception));
		String requestType = request.getHeader("X-Requested-With"); 
		ModelAndView modelAndView = null;
		if(requestType!=null){
			ExceptionVo exceptionVo = new ExceptionVo();
			exceptionVo.setCode("systemException");
			exceptionVo.setMessage("系统异常请联系管理员");
			MappingJackson2JsonView mappingJackson2JsonView = new MappingJackson2JsonView();
			modelAndView = new ModelAndView (mappingJackson2JsonView);
			modelAndView.addObject("code", exceptionVo.getCode());
			modelAndView.addObject("message", exceptionVo.getMessage());
		}else{
			modelAndView = new ModelAndView ("/404.html");
		}
		return modelAndView;
	}
}
